<!--
 * @Author: wangding
 * @Date: 2020-11-11 15:58:31
 * @LastEditTime: 2021-03-31 15:08:18
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\device\sale\add.vue
-->
<template>
    <div class="buyPage-main-box-edit">
        <el-dialog
        :title="$t('devUpgrade.VersionToUp')"
        :visible="newVisibleEdit"
        width="900px"
        @close="()=>{$store.commit('setNewVisibleEdit',false)}"
        :close-on-click-modal='false'
        >   
            <el-row :gutter="20">
                <el-col :span="19" :offset="2">
                    <el-form ref="form" :model="form" label-width="280px" >
                        <el-form-item :label="$t('equipmentClassify')" prop="type" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <el-select v-model="form.type" style="width:100%" filterable :placeholder="$t('pleaseSelect')"  @change='typeChange'>
                                <el-option
                                v-for="item in dictionaryDevice"
                                :key="item.value"
                                :label="item.code"
                                :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item v-if='form.type == 4' :label="$t('battery.firmName')" prop="editBrand" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <el-select v-model="form.editBrand" style="width:100%" filterable :placeholder="$t('pleaseSelect')"  @change='brandChange' :disabled="brandFlag">
                                <el-option
                                v-for="item in batteryList"
                                :key="item.value"
                                :label="item.code"
                                :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item v-if='form.type == 4' :label="$t('update.model')" prop="editScopeType" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <el-select v-model="form.editScopeType" style="width:100%" filterable :placeholder="$t('pleaseSelect')"  @change='scopeTypeChange'>
                                <el-option
                                v-for="item in brandValueList"
                                :key="item.value"
                                :label="item.code"
                                :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item :label="$t('version.versionAdd.versionNum')" prop="wifiSns" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <el-select v-model="form.wifiSns" style="width:100%" filterable :placeholder="$t('pleaseSelect')" @change='changeWifiSn'>
                                <el-option
                                v-for="item in valueList"
                                :key="item.taskId"
                                :label="item.longVersion"
                                :value="item.taskId">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item :label="$t('remark')" prop="remark" :rules="[{ required: false, message: $t('index_js_notNull')}]">
                            <el-input v-model="remark" type='textarea' disabled="disabled" rows="5"></el-input>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
            <span slot="footer" class="dialog-footer">
                <el-button @click="()=>{$store.commit('setNewVisibleEdit',false)}">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="save('form')">{{$t('sure')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import baseFile from '../../../components/common/baseFileInput'
export default {
    components: {
        baseFile
    },
    props: {
        addValueList:{
            type:Array,
            default:()=>{return []}
        },
        selectDataList:{
            type:Array,
            default:()=>{return []}
        },
        inverterType:{
            type:String,
            default:''
        },
        scope:{
            type:String,
            default:''
        },
        dictionaryDevice:{
            type:Array,
            default:()=>{return []}
        },
        editInverter:{
            type:Number,
            default:null
        },
        editWifiSn:{
            type:String,
            default:''
        },
        userTypeRole:{
            type:String,
            default:''
        },
        batteryList:{
            type:Array,
            default:()=>{return []}
        },
        dictionary:{
            type:Object,
            default:()=>{return {}}
        },
        rowEditData:{
            type:Object,
            default:()=>{return {}}
        }
    },
    data() {
        return {
            form:{
                wifiSns:"",
                type:"",
                editBrand:"",
                editScopeType:"",
            },
            brandValueList:[],
            cloudType:[{
                id:'0',
                text:'Solax Cloud'
            },{
                id:'1',
                text:'QCELLS'
            }],
            valueList:[],
            remark:'',
            brandFlag:false
        };
    },
    computed: {
        newVisibleEdit(){
            return this.$store.state.sysStore.newVisibleEdit
        }
    },
    watch: {
        rowEditData:{
            handler(nv,ov){
                // console.log(nv,'nv');
                // if(this.form.type == 4){
                //     if(this.rowEditData.batteryBrand){
                //         this.form.editBrand = this.rowEditData.batteryBrand
                //         this.brandFlag = true
                //     }else{
                //         this.brandFlag = false
                //     }
                // }
            },
            deep:true
        }
    },
    created() {

    },
    mounted() {

    },
    methods: {
        async updateUpdateVersion(){
            if(this.rowEditData.inverterType == 4 && this.form.type == 3){
                let oldV = parseInt(this.rowEditData.inverterVersion)
                let newV = this.returnVersion(this.form.wifiSns)
                if(oldV == 1 || oldV == 3){
                    if(!(newV == 1 || newV == 3)){
                        this.comm.showMsg(false,this.$t('wifi.arrUploadMsg'))
                        return
                    }
                }
                if(oldV == 2){
                    if(newV == 1 || newV == 3){
                        this.comm.showMsg(false,this.$t('wifi.arrUploadMsg'))
                        return
                    }
                }
            }
            let res = await this.API.updateUpdateVersion({
                wifiSns:this.form.wifiSns+"#"+this.editWifiSn
            })
            if(res.success){
                this.comm.showMsg(res.success,this.$t('devUpgrade.waitForUpgrade'))
                this.$store.commit('setNewVisibleEdit',false)
                this.$emit('uploadData')
                this.form.type = ''
                this.form.wifiSns = ''
                this.form.editBrand = ''
                this.form.editScopeType = ''
            }else{
                this.comm.showMsg(res.success)
            }
            
        },
        changeWifiSn(){
            this.valueList.forEach(item =>{
                if(item.taskId == this.form.wifiSns){
                    this.remark = item.remark
                }
            })
        },
        typeChange(){
            this.form.wifiSns = ''
            if(this.form.type != 4){
                this.updateGetUpVersion()
            }else{
                if(this.rowEditData.batteryBrand){
                    this.form.editBrand = this.rowEditData.batteryBrand
                    this.brandChange()
                    if(!(this.userTypeRole=='test')){
                        this.brandFlag = true
                    }
                    
                }
            }
        },
        brandChange(){
            if(this.form.editBrand == 81){
                this.brandValueList = this.dictionary.BATTERY_VERSION_81
            }
            if(this.form.editBrand == 82){
                this.brandValueList = this.dictionary.BATTERY_VERSION_82
            }
            if(this.form.editBrand == 83){
                this.brandValueList = this.dictionary.BATTERY_VERSION_83
            }
            if(this.form.editBrand == 84){
                this.brandValueList = this.dictionary.BATTERY_VERSION_84
            }
            if(this.form.editBrand == 129){
                this.brandValueList = this.dictionary.BATTERY_VERSION_129
            }
        },
        scopeTypeChange(){
            this.updateGetUpVersion()
            this.form.wifiSns = ''
        },
        async updateGetUpVersion(){
            let res = await this.API.updateGetUpVersion({
                wifiSN:this.editWifiSn,
                type:this.form.type,
                inverterType:this.editInverter == 11 || this.editInverter == 12 ? 10:this.editInverter,
                brand:this.form.editBrand,
                scope:this.form.editScopeType,
            })
            this.valueList = res
            if(!res.length){
                this.comm.showMsg(false,this.$t('devUpgrade.noVersionToChoose'))
            }
        },
        save(formName){
            this.$refs[formName].validate((valid) => {
              if (valid) {
                  this.updateUpdateVersion()
              } else {
                console.log('error submit!!');
                return false;
              }
            });
        },
        returnVersion(id){
            let inverterVersion = ''
            this.valueList.forEach(item =>{
                if(item.taskId == id){
                    inverterVersion = item.version
                }
            })
            return inverterVersion?parseInt(inverterVersion):''
        }
    },
};
</script>

<style  lang="less" >
.buyPage-main-box-edit{
    .el-form-item__content{
        display: flex;
    }
    .icon-fontsize{
        font-size: 25px;
        margin-left: 10px;
        display: block;
        width: 25px;
        height: 33px;
    }
    .el-textarea.is-disabled .el-textarea__inner {
        background-color: #f5f5f5;
        color: #fea429;
    }
}
</style>
